package net.csdra.article.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.csdra.article.domain.Template;
import net.csdra.article.domain.TemplateRepository;
import net.csdra.article.domain.meta.CRSMeta;
import net.csdra.article.domain.meta.DateMeta;
import net.csdra.article.domain.meta.MetaBase;
import net.csdra.article.domain.meta.NumberMeta;
import net.csdra.article.service.form.CreateTemplate;
import net.csdra.article.service.form.UpdateTemplate;
import net.csdra.basis.Utils;
import net.csdra.basis.domain.CJpaRepository;
import net.csdra.basis.service.CurrentUserInfo;
import net.csdra.basis.service.ServiceBaseInterfaceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * Created by msf on 2018/2/2.
 */
public class TemplateManageServiceImpl extends ServiceBaseInterfaceImpl<Template, CreateTemplate, UpdateTemplate> implements TemplateManageService {
    TemplateRepository jpaRepository;
    CurrentUserInfo currentUserInfo;

    public TemplateManageServiceImpl(TemplateRepository jpaRepository, CurrentUserInfo currentUserInfo) {
        super(Template.class, jpaRepository, currentUserInfo);
        this.jpaRepository = jpaRepository;
        this.currentUserInfo = currentUserInfo;
    }

    @Override
    public Template create(CreateTemplate createTemplate) {
        //TODO 完成必要断言
        Template template = Utils.createAutoSet(Template.class, this.currentUserInfo.get().getUserName());
        //TODO 完成剩余属性赋值
        template.setMetaList(getMetaToJson(createTemplate.getMetaList()));
        template.setTemplateId(template.getId());
        jpaRepository.save(template);
        Template result = new Template();
        Utils.copyProperties(template, result);
        return result;
    }

    /**
     * @param updateTemplate
     * @return
     */
    @Override
    public Template update(UpdateTemplate updateTemplate) {
        //TODO 完成必要断言
//        Template template = super.update(updateTemplate);
        Template origin = this.jpaRepository.findOne(updateTemplate.getId());
        Utils.updateAutoSet(origin, this.currentUserInfo.get().getUserName());
        //TODO 完成剩余属性赋值
        origin.setMetaList(getMetaToJson(updateTemplate.getMetaList()));
        //TODO 思考是否需要对历史版本进行保存，得出结果。暂不实现
        jpaRepository.save(origin);
        Template result = new Template();
        Utils.copyProperties(origin, result);
        return result;
    }


    private List<MetaBase> getTemplateDefaultMeta() {
        //TODO 将默认字段持久化到文件，每次直接读取文件
        CRSMeta district = new CRSMeta("select", "镇街", null, "system-district");
        CRSMeta actionType = new CRSMeta("select", "活动类型", null, "演出", "讲座", "培训", "展览", "其他");
        DateMeta beginDate = new DateMeta("开始时间", null);
        NumberMeta actionTime = new NumberMeta("活动时长", null);
        NumberMeta people = new NumberMeta("实到人数", null);
        NumberMeta money = new NumberMeta("经费", null);
        return Arrays.asList(district, actionType, beginDate, actionTime, people, money);
    }

    private String getMetaToJson(List<MetaBase> metaBases) {
        Assert.notNull(metaBases, "Meta不能为空");
        try {
            String s = new ObjectMapper().writeValueAsString(metaBases);
            return s;
        } catch (JsonProcessingException e) {
            throw new RuntimeException("传入Meta转换Json错误", e);
        }
    }

    @Override
    public List<Template> getAllHistory(String templateId) {
        //暂不实现
        throw new RuntimeException("暂不支持");
    }

    @Override
    public Page<Template> getPageByIsPublic() {
        return null;
    }

    @Override
    public Template getLatestTemplate(String templateId) {
        //暂不实现
        throw new RuntimeException("暂不支持");
    }
}
